#include <QCoreApplication>
#include "shared/common.h"
#include "server/udpsever.h"
#include "main.h"

UDPSever* udpServer;
Logg::Logger* _log;
QMap<int,QString> TL2CMD_NAMES;
QMap<int, Player*> LPlayers;
QMap<quint64, Game*> LGames;
DataServer* database;
LogonServer* LoginServer;
QMap<QTcpSocket* ,Handlers::SessionHandler*> Clients;
Config* CfgFile;
quint32 filtered=0;

unsigned int TCP_PORT;
unsigned int UDP_PORT;
unsigned int UDP_CLIENT;
bool DEBUG_TCP_SEND;
bool DEBUG_TCP_RECV;
bool DEBUG_UDP_SEND;
bool DEBUG_UDP_RECV;
unsigned short* DEBUG_TCP_FILTER;
unsigned int DEBUG_TCP_FILTER_COUNT;
unsigned short* DEBUG_UDP_FILTER;
unsigned int DEBUG_UDP_FILTER_COUNT;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    #if defined( __WIN32__ ) || defined( WIN32 ) || defined( _WIN32 )
        system("TITLE TL2GServer v1.0");
    #endif
    _log = new Logg::Logger();
    _log->start();
    _log->LogIt("TL2GServer v1.0");
    ReadConfig();

    udpServer = new UDPSever(4175);
    udpServer->start();
    return a.exec();
}

void ReadConfig()
{
    _log->LogIt("Loading config...");
    CfgFile = new Config("TL2Server.conf");
    TCP_PORT = CfgFile->pInt("PORT.TCP");
    UDP_PORT = CfgFile->pInt("PORT.UDP");
    UDP_CLIENT = CfgFile->pInt("PORT.CLIENT");

    DEBUG_TCP_SEND  = CfgFile->pBool("DEBUG.TCP.SEND");
    DEBUG_TCP_RECV  = CfgFile->pBool("DEBUG.TCP.RECV");
    DEBUG_UDP_SEND  = CfgFile->pBool("DEBUG.UDP.SEND");
    DEBUG_UDP_RECV  = CfgFile->pBool("DEBUG.UDP.RECV");

    QString spliter(CfgFile->pString("DEBUG.TCP.FILTER").c_str());
    QStringList filter=spliter.split(',');
    DEBUG_TCP_FILTER = new unsigned short[sizeof(unsigned short*) * filter.count()];
    DEBUG_TCP_FILTER_COUNT=filter.count();

    QListIterator<QString> itr (filter);
    int i = 0;
    while (itr.hasNext()) {
        QString current = itr.next();
        bool ok;
        DEBUG_TCP_FILTER[i] = current.toUShort(&ok,16);
        i++;
    }

    spliter=CfgFile->pString("DEBUG.UDP.FILTER").c_str();
    filter=spliter.split(',');
    DEBUG_UDP_FILTER = new unsigned short[sizeof(unsigned short*) * filter.count()];
    DEBUG_UDP_FILTER_COUNT=filter.count();

    itr=filter;
     i = 0;
    while (itr.hasNext()) {
        QString current = itr.next();
        bool ok;
        DEBUG_UDP_FILTER[i] = current.toUShort(&ok,16);
        i++;
    }

    _log->LogIt(QString("Server status"));
    _log->LogIt(QString("UDP    Port: %1").arg(UDP_PORT));
    _log->LogIt(QString("Client Port: %1").arg(UDP_CLIENT));
    _log->LogIt(QString("Debug config"));
    _log->LogIt(QString("UDP RECV: %1").arg(DEBUG_UDP_RECV));
    _log->LogIt(QString("UDP SEND: %1").arg(DEBUG_UDP_SEND));
    _log->LogIt(QString("Filters : %1").arg(DEBUG_TCP_FILTER_COUNT+DEBUG_UDP_FILTER_COUNT));

}
QString getTestPacket()
{
    QString filename;
    filename =QDir::currentPath();
    filename +="/testPkt.txt";
    QFile file(filename);
    if (!file.open (QIODevice::ReadOnly))
         return QString::null;
    QTextStream stream ( &file );
    QString line;
    QString pool;
    while( !stream.atEnd() ) {
         line = stream.readLine();
         line = line.split('#')[0];
         pool+=line.trimmed().remove('\n').remove('\r').remove('\t').remove(' ');
         //pool+=line.remove(' ');
    }
    file.close();
    return pool;
}
bool FILTER_TCP(quint16 cmd){
    for(int i=0;i<DEBUG_TCP_FILTER_COUNT;i++)
        if(DEBUG_TCP_FILTER[i]==cmd)
        {
            filtered++;
            #if defined( __WIN32__ ) || defined( WIN32 ) || defined( _WIN32 )
            QString tittle("TITLE TL2Server v1.0 pkt filter(%1)");
            tittle=tittle.arg(filtered);
                system(tittle.toAscii().data());
            #endif
            return false;
        }
    return true;
}
bool FILTER_UDP(quint16 cmd){
    for(int i=0;i<DEBUG_UDP_FILTER_COUNT;i++)
        if(DEBUG_UDP_FILTER[i]==cmd)
        {
            filtered++;
            #if defined( __WIN32__ ) || defined( WIN32 ) || defined( _WIN32 )
            QString tittle("TITLE TL2Server v1.0 pkt filter(%1)");
            tittle=tittle.arg(filtered);
                system(tittle.toAscii().data());
            #endif
            return false;
        }
    return true;
}
